﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.BAL;
using System.Data.EntityClient;
using WEB.DAL.Models;


namespace WpfClient.Classes
{
    public static class SyncVacanciesWithWebSite
    {
        private static EntityConnection GetConnection()
        {
            EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();

            ecsb.Name = "WebSiteEnteties";

            return new EntityConnection(ecsb.ConnectionString);
        }

        public static string AddToWebSite(DAL.Models.Vacancy vacancy)
        {
             
            try
            {
                DropFromWebSite(vacancy);

                using (WebSiteEnteties websiteEntities = new WebSiteEnteties(GetConnection()))
                {
                    vacancy.IsOnSite = true;
                    Vacancy webVacancy = new Vacancy();

                    webVacancy.vacancy_id = vacancy.Id;

                    if (vacancy.Candidate != null)
                    {
                         
                        foreach (DAL.Models.Candidat_EMails ce in vacancy.Candidate.Candidat_EMails)
                        {
                            if (ce.EMail != string.Empty)
                            webVacancy.vacancy_candidate += ce.EMail;
                        }
                    
                    }

                    if (vacancy.Candidate1 != null)
                    {
                        foreach (DAL.Models.Candidat_EMails ce in vacancy.Candidate1.Candidat_EMails)
                        {
                            if (ce.EMail != string.Empty)
                                webVacancy.vacancy_candidate1 += ce.EMail;
                        }
                    }
                    websiteEntities.Vacancies.AddObject(webVacancy);

                    websiteEntities.SaveChanges();

                    return string.Empty;
                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }

        public static string DropFromWebSite(DAL.Models.Vacancy vacancy)
        {
            try
            {
                using (WebSiteEnteties websiteEntities = new WebSiteEnteties(GetConnection()))
                {
                    var webVacancies = from n in websiteEntities.Vacancies
                                        where n.vacancy_id == vacancy.Id
                                        select n;

                    foreach (Vacancy c in webVacancies)
                    {
                        websiteEntities.Vacancies.DeleteObject(c);
                    }

                    websiteEntities.SaveChanges();

                    vacancy.IsOnSite = false;

                    return string.Empty;

                }
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
    }
}